//
//  HomeServiceDetailsIntroductionContentTwoCell.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/10/29.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "HomeServiceDetailsIntroductionContentTwoCell.h"
#import "CellFrameModel.h"
#import "HomeModel.h"
#import "ServiceModel.h"
@interface HomeServiceDetailsIntroductionContentTwoCell()
@property(nonatomic,strong) UILabel *titleL;
@property(nonatomic,strong) UILabel *subL;
@property(nonatomic,strong) NSMutableArray *temp;
@end
@implementation HomeServiceDetailsIntroductionContentTwoCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//        self.contentView.backgroundColor = [UIColor redColor];
        
    }
    return self;
}
-(void)setUpUI
{

    self.top = 10;
    self.titleL = [[UILabel alloc]initWithFrame:CGRectMake(18, self.top, 60, 23)];
//    self.titleL.text = @"可接行业";
    self.titleL.font = [UIFont systemFontOfSize:13];
    self.titleL.textColor = kLabel153Color;
    [self.contentView addSubview:self.titleL];
//    self.titleL.backgroundColor = [UIColor orangeColor];
    CGFloat marginNor= CGRectGetMaxX(self.titleL.frame)+10;
    CGFloat marginX= 10;
    self.h = 23;
    
    CGFloat y = self.top;
    CGFloat x = 0;
    CGFloat alineButWidth = 0;
    CGFloat alineButtonX = 0;

    for (int i = 0; i < self.temp.count; i++) {
//        int num = i +1;
//        if (num >=self.temp.count) {
//            num = i;
//        }
        XLog(@"self.temp[i]:%@",self.temp[i]);
        CGFloat w = [MCButton getWidthWithTitle:self.temp[i] withFontSize:13]+20;
        alineButWidth = w+marginX+alineButWidth;
        alineButtonX = CGRectGetMaxX(self.subL.frame);
        if (alineButWidth > self.contentView.frame.size.width-CGRectGetMaxX(self.titleL.frame)-marginX -18) {
            alineButWidth = w+marginX;
            y = self.top+CGRectGetMaxY(self.subL.frame);
            alineButtonX = 0;
            marginNor = CGRectGetMaxX(self.titleL.frame)+20;
        }else {
            if (i == 0) {
                marginNor = CGRectGetMaxX(self.titleL.frame)+20;
            }else {
                marginNor = 10;
            }
//            marginNor = 20;
        }
        x = alineButtonX+marginNor;
        if (i == 0) {
            XLog(@"x:%f",x);
            x = marginNor;
        }
        
        
        self.subL = [[UILabel alloc]initWithFrame:CGRectMake(x, y, w, self.h)];
        [self.contentView addSubview:self.subL];
        self.subL.tag = self.indexPath.row;
        self.subL.font = [UIFont systemFontOfSize:13];
        self.subL.textColor = kLabel102Color;
//        self.subL.backgroundColor = [UIColor orangeColor];
        self.subL.text = self.temp[i];
        self.subL.textAlignment = NSTextAlignmentCenter;
    }
    XLog(@"self.row:%ld",self.row);
    XLog(@"y:%f",y);
    self.row ++;
    self.frameModel.cellHeight = y;
    self.lineL = [[UILabel alloc]initWithFrame:CGRectMake(18, kHeight-9, kUIScreen.size.width-18*2, 9)];
    XLog(@"self.contentView.frame.size.width:%f",self.contentView.frame.size.width);
    self.lineL.font = [UIFont systemFontOfSize:13];
    self.lineL.textColor = kLineColor;
//    self.lineL.backgroundColor = [UIColor redColor];
    self.lineL.numberOfLines = 1;
    self.lineL.text = @"....................................................................................................................................................................................................................................................................................................................................................................................................................................";
    [self.contentView addSubview:self.lineL];
}
- (void)setTitleNamed:(NSString *)titleNamed
{
    _titleNamed = titleNamed;
//    titleNamed = @"可接行业";
    self.titleL.text = titleNamed;
}
- (void)setIndexPath:(NSIndexPath *)indexPath {
    _indexPath = indexPath;

    for (id obj in self.contentView.subviews) {
//        if ([obj isKindOfClass:[UILabel class]]) {
//            XLog(@"obj:%@",obj);
//            UILabel *label = (UILabel *)obj;
//            if (label.tag == self.indexPath.row) {
                [obj removeFromSuperview];
//            }else{
//
//            }
//        }
    }
}
- (void)setFrameModel:(CellFrameModel *)frameModel
{
    _frameModel = frameModel;
//    frameModel.homeModel = self.sendDemandHomeModel;
//    [self.titleL removeFromSuperview];
//    [self.subL removeFromSuperview];
//    [self.lineL removeFromSuperview];
//    _sendDemandHomeModel = sendDemandHomeModel;
    XLog(@"sendDemandHomeModel.industryList:%@",frameModel.homeModel.industryList);
    XLog(@"endtime:%ld",frameModel.homeModel.endtime);
    if (self.indexPath.row ==1) {
        [self setUpUI];
        self.titleNamed =@"需求归类";
        self.temp = [NSMutableArray arrayWithArray:frameModel.homeModel.industryList];
    }else if (self.indexPath.row == 2){
        [self setUpUI];
        self.titleNamed =@"类型细分";//associationtypesubList
        self.temp = [NSMutableArray arrayWithArray:frameModel.homeModel.associationtypesubList];
        XLog(@"self.temp:%@",self.temp);
        XLog(@"sendDemandHomeModel.associationtypesubList:%@",frameModel.homeModel.associationtypesubList);
//        XLog(@"self.temp:%@",self.temp);
    }else if (self.indexPath.row == 3){
        [self setUpUI];
        self.titleNamed =@"付款方式";
        self.temp = [NSMutableArray arrayWithArray:frameModel.homeModel.payList];
        XLog(@"frameModel.homeModel.billingList:%@",frameModel.homeModel.payList);
    }else if (self.indexPath.row == 4){
        [self setUpUI];
        self.titleNamed =@"需求地区";
        XLog(@"frameModel.homeModel.placeList:%@",frameModel.homeModel.placeList);
        self.temp = [NSMutableArray arrayWithArray:frameModel.homeModel.placeList];
        XLog(@"self.temp:%@",self.temp);
    }else if (self.indexPath.row == 5){
        [self setUpUI];
        self.titleNamed =@"过期时间";
        NSTimeInterval interval    = frameModel.homeModel.creattime/1000.0;
        NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        XLog(@"frameModel.homeModel.endtime:%ld",(long)frameModel.homeModel.endtime);
        NSString *currentDateStr = [formatter stringFromDate: date];
        XLog(@"frameModel.titles:%@",currentDateStr);
        self.temp = [NSMutableArray arrayWithObject:currentDateStr];
    }
    XLog(@"CGRectGetMaxY(self.subL.frame):%f",CGRectGetMaxY(self.subL.frame));
    self.cellHeight = CGRectGetMaxY(self.subL.frame);
    frameModel.titles = self.temp;
    XLog(@"frameModel.titles:%@",frameModel.titles);
    XLog(@"self.temp:%@",self.temp);
}
#pragma mark-:需求详情
- (void)setFrameTakeModel:(CellFrameModel *)frameTakeModel
{
    _frameTakeModel = frameTakeModel;
    if (self.indexPath.section == 1) {
        if (self.indexPath.row ==1) {
            self.temp = [NSMutableArray arrayWithArray:frameTakeModel.serviceModel.industryList];
            XLog(@"self.temp:%@",self.temp);//2
            [self setUpUI];
            self.titleNamed =@"可接行业";
        }else if (self.indexPath.row == 2){
            self.temp = [NSMutableArray arrayWithArray:frameTakeModel.serviceModel.commonIndustryList];
            [self setUpUI];
            XLog(@"self.temp:%@",self.temp);//2
            self.titleNamed =@"常做行业";
        }else if (self.indexPath.row == 3){
            self.temp = [NSMutableArray arrayWithArray:frameTakeModel.serviceModel.billingList];
            [self setUpUI];
            self.titleNamed =@"计费方式";
        }else if (self.indexPath.row == 4){
            if (frameTakeModel.serviceModel.isinvoice == 0) {
                self.temp = [NSMutableArray arrayWithArray:@[@"不开具发票"]];
            }else {
                self.temp = [NSMutableArray arrayWithArray:@[@"开发票"]];
            }
             XLog(@"self.temp:%@",self.temp);
            [self setUpUI];
            self.titleNamed =@"发票相关";
        }else if (self.indexPath.row == 5){
            
            self.temp = [NSMutableArray arrayWithArray:frameTakeModel.serviceModel.addressList];
            XLog(@"self.temp:%@",self.temp);
            [self setUpUI];
            self.titleNamed =@"合作地区";
        }else {
            NSTimeInterval interval    = frameTakeModel.serviceModel.createtime/1000.0;
            NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd "];
            NSString *currentDateStr = [formatter stringFromDate: date];
            NSTimeInterval intervalEnd    = frameTakeModel.serviceModel.endtime/1000.0;
            NSDate *dateEnd               = [NSDate dateWithTimeIntervalSince1970:intervalEnd];
            NSDateFormatter *formatterEnd = [[NSDateFormatter alloc] init];
            [formatterEnd setDateFormat:@"yyyy-MM-dd "];
            NSString *currentDateEndStr = [formatter stringFromDate: dateEnd];
//            [self.temp addObject:currentDateStr];
//            [self.temp addObject:currentDateEndStr];
            XLog(@"时间:%@",self.temp);
            if (![currentDateStr isEqualToString:currentDateEndStr]) {
                self.temp  =  [NSMutableArray arrayWithObjects:currentDateStr,@"-",currentDateEndStr, nil];
            }else {
                self.temp  =  [NSMutableArray arrayWithObject:currentDateStr];
            }
            [self setUpUI];
            self.titleNamed =@"有效时间";
        }
        XLog(@"CGRectGetMaxY(self.subL.frame):%f",CGRectGetMaxY(self.subL.frame));
        self.cellHeight = CGRectGetMaxY(self.subL.frame);
        frameTakeModel.titles = self.temp;
    }else if (self.indexPath.section == 2) {
        if (self.indexPath.row == 1) {
            self.temp = [NSMutableArray arrayWithObject:frameTakeModel.serviceModel.associationtype.length?frameTakeModel.serviceModel.associationtype:@""];
            [self setUpUI];
            self.titleNamed =@"服务归类";
        }else  if (self.indexPath.row == 2) {
            
            self.temp = [NSMutableArray arrayWithArray:frameTakeModel.serviceModel.typeSub1List];
            //        XLog(@"self.temp:%@",self.temp);
            [self setUpUI];
            self.titleNamed =@"类型细分";
        }else  if (self.indexPath.row == 3) {
           
        }
        
    }else if (self.indexPath.section == 3) {
        if (self.indexPath.row == 1) {
            self.temp = [NSMutableArray arrayWithObject:frameTakeModel.serviceModel.resource];
            //        XLog(@"self.temp:%@",self.temp);
            [self setUpUI];
//            self.titleNamed =@"业务特色";
        }
    }
    
}
-(void)removeSubL
{
    for (UILabel *label in self.subviews) {
//        if ([label isKindOfClass:[UILabel class]]) {
            [label removeFromSuperview];
//        }
    }

}
@end
